function print_doc() 
{
  window.print();
  return false;
}

function pdf()
{
	var f = $('form');
	var tmp_task_id = new Array();
	var tbl = document.getElementById('items').tBodies[0];
	var lastRow = tbl.rows.length;
	var iter = lastRow;
	var rowCount = iter;
	for(var rowIdx = 0; rowIdx < (rowCount); rowIdx++) 
		{
			x = 'items_descp' + rowIdx;		
			tmp_task_id[rowIdx] = document.getElementById(x).getAttribute("task_id");
		}
	//alert(tmp_task_id[0]);
	//alert(tmp_task_id[1]);
	var f = document.getElementById("form"); 
	var el = document.createElement("input");
	el.type = "hidden";
	el.name = "task_id[]";
	el.value = tmp_task_id;
   	f.appendChild(el);
	
	var el = document.createElement("input");
	el.type = "hidden";
	el.name = "project_id";
	el.value = tmp_task_id;
   	f.appendChild(el);
	
	
	try
		{
	f.setAttribute('action', 'insert_into_table');
	f.submit();
	alert("Database Insert Succesfull");
		
			try
		{
			f.setAttribute('action', 'pdf_generate');
			f.submit();
			alert("PDF Succesfull");
		}
	catch(e)
		{
			alert("PDF fail !!!!");
		}
		
		
		}
	catch(e)
		{
			alert("Database Insert Unsuccesfull");
		}
	

	//alert("Database Insert Succesfull");
  return false;
}


function save()
{
	var f = $('form');
	var tmp_task_id = new Array();
	var tbl = document.getElementById('items').tBodies[0];
	var lastRow = tbl.rows.length;
	var iter = lastRow;
	var rowCount = iter;
	for(var rowIdx = 0; rowIdx < (rowCount); rowIdx++) 
		{
			x = 'items_descp' + rowIdx;		
			tmp_task_id[rowIdx] = document.getElementById(x).getAttribute("task_id");
		}
	//alert(tmp_task_id[0]);
	//alert(tmp_task_id[1]);
	var f = document.getElementById("form"); 
	var el = document.createElement("input");
	el.type = "hidden";
	el.name = "task_id[]";
	el.value = tmp_task_id;
   	f.appendChild(el);
	
	var el = document.createElement("input");
	el.type = "hidden";
	el.name = "project_id";
	el.value = tmp_task_id;
   	f.appendChild(el);
	try
		{
	f.setAttribute('action', 'insert_into_table');
	f.submit();
	alert("Database Insert Succesfull");
		}
	catch(e)
		{
			alert("Database Insert Unsuccesfull");
		}
	//alert("Database Insert Succesfull");
  return false;
}






function add_row ()
	{
		var tbl = document.getElementById('items').tBodies[0];
			var lastRow = tbl.rows.length;
			var iter = lastRow ;
			var row = tbl.insertRow(lastRow);
			var rowCount = lastRow;
			
			var cell0 = row.insertCell(0);
			cell0.setAttribute('class', 'action');
			var element0 = document.createElement("div");
			cell0.appendChild(element0);
			
			var cell1 = row.insertCell(1);
			cell1.setAttribute('class', 'description');
			var element1 = document.createElement("textarea");
			element1.name = "items_descp[]";
			element1.id = 'items_descp' + iter;
			element1.setAttribute("style","overflow: hidden; height: 36px; background-color: rgb(250, 251, 251);");
			cell1.appendChild(element1);
			
			var cell2 = row.insertCell(2);
			cell2.setAttribute('class', 'rate');
			var element2 = document.createElement("input");
			element2.type = "text";
			element2.name = "items_rate[]";
			element2.id = 'items_rate' + iter;
			element2.value = "00.00"
			element2.setAttribute('onblur', 'maincalc()');
			cell2.appendChild(element2);
			
			var cell3 = row.insertCell(3);
			cell3.setAttribute('class', 'quantity');
			var element3 = document.createElement("input");
			element3.type = "text";
			element3.name = "items_qty[]";
			element3.id = 'items_qty' + iter;
			element3.value = "00:00:00"
			element3.setAttribute('onblur', 'maincalc()');
			element3.setAttribute("style","overflow: hidden;background-color: rgb(250, 251, 251);");
			cell3.appendChild(element3);
			
			var cell4 = row.insertCell(4);
			cell4.setAttribute('class', 'price');
			var element4 = document.createElement("input");
			element4.type = "text";
			element4.name = "items_prc[]";
			element4.id = 'items_prc' + iter;
			element4.value = "00.00";
			element4.setAttribute('onblur', 'maincalc()');
			cell4.appendChild(element4);
			
			var cell5 = row.insertCell(5);
			cell5.setAttribute('class', 'action');
			var element5 = document.createElement("a");
			element5.setAttribute('href', '#');
			element5.setAttribute('onclick', 'del_row(this)');
			element5.innerHTML = "[ x ]";
			cell5.appendChild(element5);
	}

function del_row(x)	
	{
		var id = x.parentNode.parentNode.rowIndex;
		document.getElementById("items").deleteRow(id);
	}	

function validateStartTime(tmp) 
	{  
		var time  = tmp;
		var valid  = true;  
		var spaceIndex = time.indexOf("");  
		//alert("Space Index " + spaceIndex);
		var onlyTime = time.substring(spaceIndex);   
		//alert("onlyTime " + onlyTime);
		var timeData = onlyTime.split(":");    
		//alert("timeData " + timeData);
		var hour  = (timeData[0]);  
		//alert("hour " + hour);
		var min  = (timeData[1]);    
		//alert("min " + min);
		var sec  = (timeData[2]);      
		//alert("sec " + sec);
		var regForTime = new RegExp("\\d{1,2}:\\d{1,2}:\\d{1,2}$");  
		if(!regForTime.test(onlyTime)) 
			{
			valid = false;  
			//alert("Regx False")
			}
		else if((hour < 0) || (hour > 24)) 
			{
			valid = false;  
			//alert("false hour")
			}
		else if((min < 0) || (min > 59)) 
			{
			valid = false;  
			//alert("false min")
			}
		else if((sec < 0) || (sec > 59)) 
			{
			valid = false;  
			//alert("false second")
			}
		return valid;  
	}
	
function convert_time_to_sec(tmp) 
	{  
		var time  = tmp;
		var valid  = true;  
		var spaceIndex = time.indexOf("");  
		//alert("Space Index " + spaceIndex);
		var onlyTime = time.substring(spaceIndex);   
		//alert("onlyTime " + onlyTime);
		var timeData = onlyTime.split(":");    
		//alert("timeData " + timeData);
		var hour  = (timeData[0]);  
		//alert("hour " + hour);
		var min  = (timeData[1]);    
		//alert("min " + min);
		var sec  = (timeData[2]);      
		//alert("sec " + sec);
		var regForTime = new RegExp("\\d{1,2}:\\d{1,2}:\\d{1,2}$");  
		var ttl = parseInt((hour*3600)) + parseInt((min*60)) + parseInt(sec);
		return ttl;  
	}
	
function maincalc() 
	{	
		var tmp1 = 0;
		var tmp2 = 0;
		var tbl = document.getElementById('items').tBodies[0];
		var lastRow = tbl.rows.length;
		var iter = lastRow;
		var rowCount = iter;
		//alert('LastRo:' + lastRow);
		//alert('rowCount' + iter);
		for(var rowIdx = 0; rowIdx < (rowCount); rowIdx++) 
			{
				x = 'items_rate' + rowIdx;
				y = 'items_qty' + rowIdx;
				z = 'items_prc' + rowIdx;
				//alert(x);
				tmp1 = document.getElementById(x).value;
				//alert(tmp1);
				if(tmp1 == "")
					document.getElementById(x).value = "00.0";
				if(isNaN(tmp1))
					{
						alert('Rate\'s value should be a number.');
						break;
					}
				tmp1 = document.getElementById(x).value;	
				tmp1 = parseFloat(tmp1);
				//alert(tmp1);	
				tmp2 = document.getElementById(y).value;
					if(tmp2 == "")
					document.getElementById(y).value = "00:00:00";
				if(!validateStartTime(tmp2))
					{
					alert('Hour\'s value is not valid.');
					break;
					}
				//tmp2 = parseFloat(tmp2);	
				var tmp3 = convert_time_to_sec(tmp2);
				//alert(tmp3);
				//alert(tmp1);
				tmp1 = parseFloat((tmp1/3600));
				//alert("Cost Per second : " + tmp1);
				tmp1 = tmp1*tmp3;
				//alert("Cost : " + tmp1);
				tmp1 = parseFloat(tmp1).toFixed(2);
				//alert("Cost Parsed : " + tmp1);
				//alert(tmp1);
				document.getElementById(z).value = tmp1;
				
			}
			
		fnd_ttl();
		tax();
		ttl();	
    }
	
function fnd_ttl()
	{
		var tbl = document.getElementById('items').tBodies[0];
		var lastRow = tbl.rows.length;
		var iter = lastRow ;
		var rowCount = iter;
		var ztotal = 0;
		for(var rowIdx = 0; rowIdx < (rowCount); rowIdx++) 
			{
				z = 'items_prc' + rowIdx;
				tmp2 = document.getElementById(z).value;
				tmp2 = parseFloat(tmp2);
				ztotal = parseFloat(ztotal);
				ztotal = ztotal + tmp2;
				ztotal = parseFloat(ztotal).toFixed(2);
				document.getElementById('subttl_spn').innerHTML = ztotal;
				document.getElementById('subttl').value = ztotal;
				
			}
		
	}	

function tax()
{
	var subttl = document.getElementById('subttl').value;
	subttl = parseFloat(subttl);
	var tmp = document.getElementById('tax_percentage').value;
	tmp = parseFloat(tmp);
	var tmp1 = (tmp/100);
	tmp1 = parseFloat(tmp1);
	var tmp2 = tmp1*subttl;
	tmp2 = parseFloat(tmp2).toFixed(2);
	document.getElementById('tax_amount').value = tmp2;
	document.getElementById('tax_amount_spn').innerHTML = tmp2;
}	

function ttl()
{
	var tmp = document.getElementById('tax_amount').value;
	tmp = parseFloat(tmp);
	var tmp1 = document.getElementById('subttl').value;
	//alert("subtotal:" + subttl);
	tmp1 = parseFloat(tmp1);
	var tmp2 = tmp + tmp1;
	tmp2 = parseFloat(tmp2).toFixed(2);
	
	var tmp = document.getElementById('discount').value;
	tmp = parseFloat(tmp);
	
	
	var tmp2 = tmp2 - tmp;
	tmp2 = parseFloat(tmp2).toFixed(2);
	document.getElementById('total').value = tmp2;
	document.getElementById('total_spn').innerHTML = tmp2;
}